package com.video.video.service.impl;

import ch.qos.logback.core.util.TimeUtil;
import com.video.common.exception.ServiceException;
import com.video.common.utils.StringUtils;
import com.video.common.utils.uuid.IdUtils;
import com.video.video.domain.DGoodsBach;
import com.video.video.domain.DSystemSettings;
import com.video.video.domain.DUser;
import com.video.video.domain.app.dto.DGoodsDto;
import com.video.video.domain.app.dto.GooodFrontQuery;
import com.video.video.domain.vo.GoodsQuery;
import com.video.video.service.IDGroupItemService;
import com.video.video.service.IDSystemSettingsService;
import com.video.video.service.IDUserService;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
import com.video.common.utils.DateUtils;
import javax.annotation.Resource;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.video.video.mapper.DGoodsMapper;
import com.video.video.domain.DGoods;
import com.video.video.service.IDGoodsService;

/**
 * 商品Service业务层处理
 *
 * @author ruoyi
 * @date 2022-09-27
 */
@Service
public class DGoodsServiceImpl implements IDGoodsService
{
    private static final Logger log = LoggerFactory.getLogger(DGoodsServiceImpl.class);
    @Autowired
    private DGoodsMapper dGoodsMapper;
    @Resource
    private IDSystemSettingsService idSystemSettingsService;
    @Resource
    private IDUserService idUserService;
    @Autowired
    private IDGroupItemService idGroupItemService;

    /**
     * 查询商品
     *
     * @param id 商品主键
     * @return 商品
     */
    @Override
    public DGoods selectDGoodsById(String id)
    {
        return dGoodsMapper.selectDGoodsById(id);
    }

    /**
     * 查询商品列表
     *
     * @param dGoods 商品
     * @return 商品
     */
    @Override
    public List<DGoods> selectDGoodsList(GoodsQuery dGoods)
    {
        return dGoodsMapper.selectDGoodsList(dGoods);
    }


    /**
     * 当前时间前一个月
     * @return
     */
    @Override
    public List<DGoods> selectDGoodsSoldList() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.MONTH,-1);
        String times = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());

        System.out.println("前一个月:"+times);
        return dGoodsMapper.selectDGoodsSoldList(times);
    }

    @Override
    public List<DGoodsDto> selectDGoodsDtoList(GooodFrontQuery query) {
        List<DGoodsDto> list= dGoodsMapper.selectDGoodsDtoList(query);
        List<DSystemSettings> list2 =idSystemSettingsService.selectDSystemSettingsList(new DSystemSettings());
        DSystemSettings settings=list2.get(0);
        DUser user= idUserService.selectDUserById(query.getUserId());
        if(user == null){
            throw new ServiceException("会员不存在");
        }
        //会员等级：1 一般用户2 VIP用户3 SVIP 4 代理（不优惠） 5 合伙人(不优惠、不返佣)
        BigDecimal rate=new BigDecimal("1");//会员折扣
        String flgcoom="1";//佣金标识
        if(user.getLevel().equals("1")){
            flgcoom="2";
        }else if(user.getLevel().equals("2")){
            rate=settings.getVipRate();
            flgcoom="2";
        }else if(user.getLevel().equals("3")){
            rate=settings.getSvipRate();
            flgcoom="2";
        }else if(user.getLevel().equals("4")){
            flgcoom="2";
        }
        Integer[] num={1,2,3,4,5};
        if(CollectionUtils.isNotEmpty(list)){
            for(DGoodsDto dto : list){

                //计算佣金
                if(flgcoom.equals("2")){
                    log.info("id:"+dto.getId()+" SellPrice:"+dto.getSellPrice()+" rate:"+rate);
                    //计算会员价
                    dto.setSellPrice(dto.getSellPrice().multiply(rate));
                    log.info("SellPrice:"+dto.getSellPrice()+" OneRate:"+settings.getOneRate());
                    dto.setCommissionPrice(dto.getSellPrice().multiply(settings.getOneRate()));
                }else{
                    dto.setCommissionPrice(new BigDecimal("0"));
                }

                //随机商品剩余个数
                int flg=  StringUtils.randomNum(num);
                int randomnum=1000;
                if(flg ==1){
                    randomnum=1;
                }else if(flg ==2){
                    randomnum=10;
                }else if(flg ==3){
                    randomnum=100;
                }else if(flg ==4){
                    randomnum=1000;
                }
                dto.setGoodsNum((int)((Math.random()*9+1)*1000));
            }
        }
        return list;
    }

    @Override
    public DGoodsDto getGoodsDetails(String userId, String goodId) {
       DGoods goods= dGoodsMapper.selectDGoodsById(goodId);
       if(goods == null){
           throw new ServiceException("商品已下架");
       }
        List<DSystemSettings> list2 =idSystemSettingsService.selectDSystemSettingsList(new DSystemSettings());
        DSystemSettings settings=list2.get(0);
        DUser user= idUserService.selectDUserById(userId);
        if(user == null){
            throw new ServiceException("会员不存在");
        }
        //会员等级：1 一般用户2 VIP用户3 SVIP 4 代理（不优惠） 5 合伙人(不优惠、不返佣)
        BigDecimal rate=new BigDecimal("1");//会员折扣
        String flgcoom="1";//佣金标识
        if(user.getLevel().equals("1")){
            flgcoom="2";
        }else if(user.getLevel().equals("2")){
            rate=settings.getVipRate();
            flgcoom="2";
        }else if(user.getLevel().equals("3")){
            rate=settings.getSvipRate();
            flgcoom="2";
        }else if(user.getLevel().equals("4")){
            flgcoom="2";
        }
        Integer[] num={1,2,3,4,5};

        DGoodsDto dto=new DGoodsDto();
        dto.setId(goods.getId());
        dto.setGoodName(goods.getGoodName());
        dto.setImageUrl(goods.getImageUrl());
        dto.setVideoUrl(goods.getVideoUrl());
        dto.setTestTime(goods.getTestTime());
        dto.setDownloadFlg(goods.getDownloadFlg());
        dto.setStatus(goods.getStatus());

        //计算佣金
        if(flgcoom.equals("2")){
            //计算会员价
            dto.setSellPrice(goods.getSellPrice().multiply(rate));
            dto.setCommissionPrice(goods.getSellPrice().multiply(settings.getOneRate()));
        }else{
            dto.setCommissionPrice(new BigDecimal("0"));
        }
        //随机商品剩余个数
        int flg=  StringUtils.randomNum(num);
        int randomnum=1000;
        if(flg ==1){
            randomnum=1;
        }else if(flg ==2){
            randomnum=10;
        }else if(flg ==3){
            randomnum=100;
        }else if(flg ==4){
            randomnum=1000;
        }
        dto.setGoodsNum((int)((Math.random()*9+1)*1000));
        return dto;
    }

    @Override
    public List<DGoodsDto> selectDGoodsDtoByGroupId(String groupId,String userId) {
        List<DGoodsDto>  list=dGoodsMapper.selectDGoodsDtoByGroupId(groupId);
        List<DSystemSettings> list2 =idSystemSettingsService.selectDSystemSettingsList(new DSystemSettings());
        DSystemSettings settings=list2.get(0);
        DUser user= idUserService.selectDUserById(userId);
        if(user == null){
            throw new ServiceException("会员不存在");
        }
        //会员等级：1 一般用户2 VIP用户3 SVIP 4 代理（不优惠） 5 合伙人(不优惠、不返佣)
        BigDecimal rate=new BigDecimal("1");//会员折扣
        String flgcoom="1";//佣金标识
        if(user.getLevel().equals("1")){
            flgcoom="2";
        }else if(user.getLevel().equals("2")){
            rate=settings.getVipRate();
            flgcoom="2";
        }else if(user.getLevel().equals("3")){
            rate=settings.getSvipRate();
            flgcoom="2";
        }else if(user.getLevel().equals("4")){
            flgcoom="2";
        }
        Integer[] num={1,2,3,4,5};
        if(CollectionUtils.isNotEmpty(list)){
            for(DGoodsDto dto : list){
                //计算佣金
                if(flgcoom.equals("2")){
                    //计算会员价
                    dto.setSellPrice(dto.getSellPrice().multiply(rate));
                    dto.setCommissionPrice(dto.getSellPrice().multiply(settings.getOneRate()));
                }else{
                    dto.setCommissionPrice(new BigDecimal("0"));
                }

                //随机商品剩余个数
                int flg=  StringUtils.randomNum(num);
                int randomnum=1000;
                if(flg ==1){
                    randomnum=1;
                }else if(flg ==2){
                    randomnum=10;
                }else if(flg ==3){
                    randomnum=100;
                }else if(flg ==4){
                    randomnum=1000;
                }
                dto.setGoodsNum((int)((Math.random()*9+1)*1000));
            }
        }
        return list;
    }

    /**
     * 新增商品
     *
     * @param dGoods 商品
     * @return 结果
     */
    @Override
    public int insertDGoods(DGoods dGoods)
    {
        dGoods.setId(IdUtils.getId());
        dGoods.setCreateTime(DateUtils.getNowDate());
        return dGoodsMapper.insertDGoods(dGoods);
    }

    /**
     * 修改商品
     *
     * @param dGoods 商品
     * @return 结果
     */
    @Override
    public int updateDGoods(DGoods dGoods)
    {
        dGoods.setUpdateTime(DateUtils.getNowDate());
        return dGoodsMapper.updateDGoods(dGoods);
    }

    @Override
    public int updateDGoodsBach(DGoodsBach dGoodsBach) {
        String[] ids=dGoodsBach.getId();
        for (String id : ids) {
            DGoods dGoods=new DGoods();
            dGoods.setId(id);
            dGoods.setCid(dGoodsBach.getCid());
            dGoods.setCid2(dGoodsBach.getCid2());
            dGoods.setStatus(dGoodsBach.getStatus());
            dGoods.setUpdateTime(DateUtils.getNowDate());
            dGoodsMapper.updateDGoods(dGoods);
        }
        return 1;
    }

    /**
     * 批量删除商品
     *
     * @param ids 需要删除的商品主键
     * @return 结果
     */
    @Override
    public int deleteDGoodsByIds(String[] ids)
    {
        //删除分组下的商品
        idGroupItemService.deleteDGroupItemByGoodid(ids);
        return dGoodsMapper.deleteDGoodsByIds(ids);
    }

    @Override
    public int updateCidByIds(List<String> ids,String cid)
    {
        return dGoodsMapper.updateCidByIds(cid,ids);
    }



    @Override
    public int deleteDGoodsByCIds(String[] cids) {
        return dGoodsMapper.deleteDGoodsByCIds(cids);
    }

    /**
     * 删除商品信息
     *
     * @param id 商品主键
     * @return 结果
     */
    @Override
    public int deleteDGoodsById(String id)
    {
        return dGoodsMapper.deleteDGoodsById(id);
    }
}
